<?php
	
	if(!isset($_SESSION['username'])){
		echo "<script>alert('请登录！');window.location.href='login.php?m=admin&c=manage'</script>";die;
	}
//	var_dump($_POST);die;

	$aid = $_GET['aid'];

	
	//提交
	if($_POST){
//		var_dump($_POST) ; die;
		if($_POST['title'] == ''){
			echo "<script>alert('标题不可为空');</script>";
		}elseif($_POST['remark'] == ''){
			echo "<script>alert('摘要不可为空');</script>";
		}elseif(!isset($_POST['content'])){
			echo "<script>alert('内容不可为空');</script>";
		}else{
			$con = connect('localhost','root','','login');
			//获取数据
			$data['title'] = htmlspecialchars(trim($_POST['title']));
			$data['remark'] = htmlspecialchars(trim($_POST['remark']));
			
			$content = ltrim($_POST['content'],'<p>');
			$cont = rtrim($content,'</p>');
			$data['content'] = htmlspecialchars_decode(trim($cont,''));

			$data['home'] = $_POST['home'];
			$data['edit'] = $_SESSION['nicheng'];
			$data['create_time'] = time();
			$data['category_id'] = $_POST['category_id'];
			



			
			//图片上传

			if(!empty($_FILES['imgsrc'])){
				$imgsrc = $_FILES['imgsrc'];
				$path = uploadsIMG($imgsrc);

				$img = overSize_thumb($path);
				$data['img_path'] = json_encode($img);
			}
			

			//水印
			$watertype = $_POST['watertype']; //水印类型
			if($watertype==1){

				$water = $_FILES['wig'];
				$watermark = uploadsIMG($water);
				watermark($img,$watermark);

			}else if($watertype==2){

				$text = $_POST['wt'];
				textwater($img,$text);

			}
			
			$query = dbinsert($con,'news',$data);
			
			if($query){
				echo "<script>alert('添加成功');window.location.href = 'index.php?m=admin&c=news&mid=20&aid=$aid'</script>";
			}else{
				echo "<script>alert('添加失败');window.location.href = 'index.php?m=admin&c=news&v=new_insert'</script>";
			}
		}
		//添加
		//  dbinsert($con,$table,$field)
		



		
		
		
	}	
		//新闻分类

		//dbselect($con,$table,$field='*',$where='',$order='',$limit='')
		$news_cate = dbselect($con,'category','category_name,id','pid=3');
//	die;


	assign('con',$con);

	assign('news_cate',$news_cate);

	//权限管理
	authonity($con);
?>